package com.tc.server.handler;

import com.tc.msg.message.GroupMembersRequestMessage;
import com.tc.msg.message.GroupMembersResponseMessage;
import com.tc.server.session.GroupSessionFactory;
import com.tc.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Set;

/**
 * @Auther: tianchao
 * @Date: 2022/2/16 11:08
 * @Description:
 */
@ChannelHandler.Sharable // 没有共享变量、没有状态信息
@Slf4j
public class GroupMembersRequestMessageHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage msg) throws Exception {
        String groupName = msg.getGroupName();
        Set<String> members = GroupSessionFactory.getGroupSession().getMembers(groupName);
        ctx.writeAndFlush(new GroupMembersResponseMessage(members));
    }
}
